﻿using System;
using System.Web;
using System.Web.Mvc;

using BrnMall.Core;
using BrnMall.Services;
using BrnMall.Web.Framework;
using BrnMall.Web.StoreAdmin.Models;
using System.Collections.Generic;

namespace BrnMall.Web.StoreAdmin.Controllers
{
    /// <summary>
    /// 店铺后台分类控制器类
    /// </summary>
    public partial class CategoryController : BaseStoreAdminController
    {
        /// <summary>
        /// 获得分类的属性及其值json列表
        /// </summary>
        /// <param name="cateId">分类id</param>
        /// <returns></returns>
        public ContentResult AAndVJsonList(int cateId = -1)
        {
            return Content(AdminCategories.GetCategoryAAndVListJsonCache(cateId));
        }

        /// <summary>
        /// 添加属性
        /// </summary>
        [HttpPost]
        public ActionResult AddAttribute(AttributeModel model, int cateId = -1)
        {
            CategoryInfo categoryInfo = AdminCategories.GetCategoryById(cateId);
            if (categoryInfo == null)
                return Content("分类不存在");

            if (AdminCategories.GetAttrIdByCateIdAndName(cateId, model.AttributName) > 0)
                return Content("名称已经存在");

            List<AttributeGroupInfo> attrGroupList = AdminCategories.GetAttributeGroupListByCateId(cateId);
            if (attrGroupList == null || attrGroupList.Count == 0)
                return Content("商品分类的属性组不存在");

            AttributeGroupInfo attributeGroupInfo = attrGroupList[0];
            model.AttrGroupId = attributeGroupInfo.AttrGroupId;

            if (ModelState.IsValid)
            {
                AttributeInfo attributeInfo = new AttributeInfo();
                attributeInfo.Name = model.AttributName;
                attributeInfo.CateId = cateId;
                attributeInfo.AttrGroupId = model.AttrGroupId;
                attributeInfo.ShowType = model.ShowType;
                attributeInfo.IsFilter = model.IsFilter;
                attributeInfo.DisplayOrder = model.DisplayOrder;

                AdminCategories.CreateAttribute(attributeInfo, attributeGroupInfo);
                AddStoreAdminLog("添加分类属性", "添加分类属性,属性为:" + model.AttributName);
                return Content("success");
            }
            return Content("添加失败");
        }

        /// <summary>
        /// 获取属性分组选择列表
        /// </summary>
        /// <param name="cateId">分类id</param>
        /// <returns></returns>
        private List<SelectListItem> GetAttributeGroupSelectList(int cateId)
        {
            List<SelectListItem> itemList = new List<SelectListItem>();
            List<AttributeGroupInfo> attributeGroupList = AdminCategories.GetAttributeGroupListByCateId(cateId);
            itemList.Add(new SelectListItem() { Text = "请选择分类", Value = "0" });
            foreach (AttributeGroupInfo attributeGroupInfo in attributeGroupList)
            {
                itemList.Add(new SelectListItem() { Text = attributeGroupInfo.Name, Value = attributeGroupInfo.AttrGroupId.ToString() });
            }
            return itemList;
        }
    }
}

